﻿using Passbook.Generator;
using Passbook.Generator.Fields;
using Passbook.Sample.Web.Database.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;

namespace Passbook.Sample.Web.Database
{
    public class PassController : Database
    {
        private void AddLocationsToPass(PassGeneratorRequest rPass, IEnumerable<Location> locations)
        {
            foreach (var l in locations)
            {
                rPass.Locations.Add(new Generator.Location
                {
                    Latitude = l.Latitude,
                    Longitude = l.Longtitude,
                    RelevantText = l.ReleventText
                });
            }
        }

        public IEnumerable<Location> GetAllLocations(DatabaseDataContext db, int passID)
        {            
            var locations = from p in db.PassLocations
                            where p.PassID == passID
                            select p.Location;
            return locations;
        }

        public IEnumerable<Location> Update(DatabaseDataContext db, List<Generator.Location> locations)
        {
            // todo: later;
            return null;
        }


        public PassGeneratorRequest GetPass(DatabaseDataContext db, string serialNumber)
        {
            
            Pass pass = db.Passes.Single(p => p.SerialNumber == serialNumber);
            PassGeneratorRequest rPass = null;
            //{
            //    // Case to correct type;
            //}
            /*
            var locations = from p in pass.PassLocations
                            select p.Location;

            AddLocationsToPass(rPass, locations);
            */
            return rPass;
        }

        private static string GenerateSerialNumber()
        {
            //TODO: later;            
            return "123";
        }

        public void CreatePassWebServiceUrl(DatabaseDataContext db, string url)
        {
            db.PassWebServiceUrls.InsertOnSubmit(
                new PassWebServiceUrl() { WebServiceURL = url }
                );
            db.SubmitChanges();
        }

        public void EditPassWebServiceUrl(DatabaseDataContext db, int id, string url)
        {            
            var data = db.PassWebServiceUrls.Single(w => w.ID == id);
            data.WebServiceURL = url;
            db.SubmitChanges();
        }


        private PassWebServiceUrl GetLatestPassWebServiceUrl(DatabaseDataContext db)
        {
            var db = GetDataContext();
            var data = db.PassWebServiceUrls.First(x => x.ID == db.PassWebServiceUrls.Max(x2 => x2.ID));
            return data;
        }

        public void CreatePass(DatabaseDataContext db, PassGeneratorRequest pass, IEnumerable<Location> listLocations)
        {            
            pass.SerialNumber = GenerateSerialNumber();
            AddLocationsToPass(pass, listLocations);

            //var PassTypeID = GetPassTypeID(pass);

            db.Passes.InsertOnSubmit(new Pass()
            {
                SerialNumber = pass.SerialNumber,
                // PassTypeID
                FormatVersion = pass.FormatVersion,
                OrganizationName = pass.OrganizationName,
                Description = pass.Description,
                PassWebServiceUrl = GetLatestPassWebServiceUrl(db)
            });

            // TODO: Data of pass here!

            db.SubmitChanges();
        }

        public void UpdatePass(DatabaseDataContext db, string serialNumber, PassGeneratorRequest pass)
        {
            pass.SerialNumber = serialNumber;
            // TODO: Later !
        }

        public PassType CreatePassType(DatabaseDataContext db, string teamID, string passTypeID) {
            PassType passType = new PassType() { 
                PassTypeIdentifier = passTypeID,
                TeamIdentifier = teamID
            };
            db.PassTypes.InsertOnSubmit(passType);
            db.SubmitChanges();
            return passType;
        }
    }
}
